package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.entity.TouSuJianYi;
import com.yl.auth.model.vo.TouSuJianYiVo;
import com.yl.auth.service.LaorenService;
import com.yl.auth.service.TouSuJianYiService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2023-06-28
 */
@RestController
@RequestMapping("/admin/yanglao/tousujianyi")
@Api(tags = "投诉建议信息管理")
public class TouSuJianYiController {
    @Resource
    private TouSuJianYiService touSuJianYiService;

    @Resource
    private LaorenService laorenService;
    @ApiOperation("全查+分页+条件查询")
    @GetMapping("/findAllConAndPage/{page}/{limit}")
    public Result findAllConAndPage(@PathVariable Integer page,
                                    @PathVariable Integer limit,
                                    TouSuJianYiVo touSuJianYiVo){
        Page<TouSuJianYi> page1 = new Page<>(page,limit);
        String title = touSuJianYiVo.getTitle();
        String feedbackChannels = touSuJianYiVo.getFeedbackChannels();
        Integer status = touSuJianYiVo.getStatus();
        LambdaQueryWrapper<TouSuJianYi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (touSuJianYiVo!=null){
            if (!StringUtils.isEmpty(title)){
                lambdaQueryWrapper.like(TouSuJianYi::getTitle,title);
            }
            if (!StringUtils.isEmpty(feedbackChannels)){
                lambdaQueryWrapper.eq(TouSuJianYi::getFeedbackChannels,feedbackChannels);
            }
            if (!StringUtils.isEmpty(status)){
                lambdaQueryWrapper.eq(TouSuJianYi::getStatus,status);
            }
        }
        lambdaQueryWrapper.orderByDesc(TouSuJianYi::getId);
        Page<TouSuJianYi> touSuJianYiPage = touSuJianYiService.page(page1, lambdaQueryWrapper);
        return Result.ok(touSuJianYiPage);
    }

    @ApiOperation("根据Id查看投诉建议信息详情")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
        TouSuJianYi touSuJianYi = touSuJianYiService.getById(id);
        Long laorenId = touSuJianYi.getLaorenId();
        Laoren laoren = laorenService.getById(laorenId);
        Map<String,Object> map = new HashMap<>();
        map.put("laoren",laoren);
        map.put("touSuJianYi",touSuJianYi);
        return Result.ok(map);
    }
    @ApiOperation("标记处理")
    @GetMapping("/handleById/{id}")
    public Result handleById(@PathVariable Integer id){
        TouSuJianYi touSuJianYi = touSuJianYiService.getById(id);
        //当标记处理时状态字段改为已处理 完成处理时间为当前时间
        touSuJianYi.setStatus(1);
        touSuJianYi.setProcessingTime(new Date());
        touSuJianYiService.updateById(touSuJianYi);
        return Result.ok(touSuJianYi);
    }
    @ApiOperation("查看指定投诉建议信息")
    @GetMapping("/getEditById/{id}")
    public Result getEditById(@PathVariable Integer id){
        TouSuJianYi touSuJianYi = touSuJianYiService.getById(id);
        return Result.ok(touSuJianYi);
    }

    @ApiOperation("修改投诉建议信息")
    @PostMapping("/update")
    public Result update(@RequestBody TouSuJianYi touSuJianYi){
        touSuJianYiService.updateById(touSuJianYi);
        return Result.ok();
    }
}

